package com.qianfeng.openapi.controller;

import com.github.pagehelper.PageInfo;
import com.qianfeng.openapi.bean.AjaxMessage;
import com.qianfeng.openapi.bean.TableData;
import com.qianfeng.openapi.pojo.Customer;
import com.qianfeng.openapi.service.CustomerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/sys/customer")
public class CustomerController {
    @Autowired
    private CustomerService service;
    @RequestMapping("/table")
    public TableData<Customer> getCustomerList(int page, int limit, String name, Integer state ){
        Customer customer=new Customer();
        customer.setUsername(name);
        customer.setState(state);
        PageInfo<Customer> pageInfo = service.selectCustomerWhere(page, limit, customer);
        TableData<Customer> tapeData=new TableData<>();
        tapeData.setCode(0);
        tapeData.setMsg("ok");
        tapeData.setCount(pageInfo.getTotal());
        tapeData.setData(pageInfo.getList());
        return tapeData;
    }
    @RequestMapping("/add")
    public AjaxMessage insertCustomer(Customer customer){
        service.insertCustomer(customer);
        return new AjaxMessage(true,"添加成功");
    }
    @RequestMapping("/update")
    public AjaxMessage updateCustomer(Customer customer){
        service.updateCustomer(customer);
        return new AjaxMessage(true,"更改成功");
    }
    @RequestMapping("del")
    public AjaxMessage deleteCustomer(int[] ids){
        service.deleteCustomer(ids);
        return new AjaxMessage(true,"删除成功");
    }
    @RequestMapping("/tree")
    public List<Customer> tree(){

        return service.getAllCustomer();
    }

    @RequestMapping("/info")
    public Customer info(int id) {

        return service.selectCustomerById(id);
    }

}
